/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.factory.server.bo;

import com.factory.server.dto.UserDTO;
import com.factory.server.provider.HashProvider;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;

/**
 *
 * @author milos
 */
@Configurable(preConstruction = true)
@Entity
@Table(name = "users")
public class User extends AbstractBusinessObject {

    private static final long serialVersionUID = 1L;
    @Column(name = "LOGIN", length = 30)
    private String login;
    @Column(name = "PASSWORD", length = 40)
    private String password;
    @Column(length = 40, nullable = false) //40 je hash od SHA1
    private String salt;
    @Column(name = "EMAIL", length = 60, unique = true)
    private String email;
    private boolean blocked;
    private boolean admin;
    @Autowired
    private transient HashProvider hashProvider;

    public User() {
    }

    public User(UserDTO userDTO) {
        this.login = userDTO.getLogin();
        this.email = userDTO.getEmail();
        setPassword(userDTO.getPassHash());
//        this.password = hashProvider.computeHash(userDTO.getPassHash());
        this.blocked = userDTO.isBlocked();
        this.admin = userDTO.isIsAdmin();

    }

    public User(Long id) {
        this.id = id;
    }

    public boolean isAdmin() {
        return admin;
    }

    public void setAdmin(boolean admin) {
        this.admin = admin;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getPassword() {
        return password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof User)) {
            return false;
        }
        User other = (User) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.factory.web.bo.User[ id=" + id + " ]";
    }

    public boolean isBlocked() {
        return blocked;
    }

    public void setBlocked(boolean blocked) {
        this.blocked = blocked;
    }

    public boolean hasPassword(String password) {
        if (hashProvider.computeHash(password).equals(this.password)) {
            return true;
        }
        if (this.password.equals(password)) {
            return true;
        }
        return false;
    }

    public void setPassword(String password) {
        this.salt = hashProvider.computeHash(System.nanoTime() + "");
        this.password = hashProvider.computeHash(password + salt);
    }

    public String getSalt() {
        return salt;
    }

    public void setSalt(String salt) {
        this.salt = salt;
    }
}
